﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fcdbas.Core;
using Fcdbas.Biz;
namespace Fcdbas.WinUI.Common
{
    public sealed class Security
    {
        internal static SalBoolean IsMethodAvaliable(SalString identity_ ,SalString procedure_name_ )
        {
            if (AppEnvironment.Session.IsRoot || cSessionManager.IsRemote)
            {
                
                return true;
            }
            //SalString sSql = "SELECT SecuritySys_IsMethodAvaliable('" + identity_ + "','" + procedure_name_ + "');";
            //return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
            return IsDBAuthorize(identity_, procedure_name_, SecObjectType.Procedure);

        }
        public static SalBoolean IsMethodAvaliable(Type formType, SalString procedure_name_,SalString desc_  )
        {
            if (AppEnvironment.Session.IsRoot )
            {
                fnd_object_security objSec = FndObjectFactory.CreateObjectSecurity(formType, procedure_name_, SecObjectType.Database, SecObjectType.Procedure, desc_, "False");
                if (FndDAL.FindObjectSecurity(objSec) == null)
                {
                    FndDAL.ObjectSecurityNew(objSec);
                }
                return true;
            }
            //SalString sSql = "SELECT SecuritySys_IsMethodAvaliable('" + identity_ + "','" + procedure_name_ + "');";
            //return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
            return IsDBAuthorize(AppEnvironment.Session.USER_ID, procedure_name_, SecObjectType.Procedure);

        }

        internal static SalBoolean IsFunctionAvaliable(SalString identity_, SalString function_name_)
        {
            if (AppEnvironment.Session.IsRoot || cSessionManager.IsRemote)
                return true;
            //SalString sSql = "SELECT SecuritySys_IsFunctionAvaliable('" + identity_ + "','" + function_name_ + "');";
            //return cSessionManager.GetUserFunctionValue<bool>(sSql,false);
            return IsDBAuthorize(identity_, function_name_, SecObjectType.Function);
        }

        public static SalBoolean IsFunctionAvaliable(Type formType, SalString function_name_,SalString desc_)
        {
            if (AppEnvironment.Session.IsRoot )
            {
                fnd_object_security objSec = FndObjectFactory.CreateObjectSecurity(formType, function_name_, SecObjectType.Database, SecObjectType.Function, desc_, "False");
                if (FndDAL.FindObjectSecurity(objSec) == null)
                {
                    FndDAL.ObjectSecurityNew(objSec);
                }
                return true;
            }
            //SalString sSql = "SELECT SecuritySys_IsFunctionAvaliable('" + identity_ + "','" + function_name_ + "');";
            //return cSessionManager.GetUserFunctionValue<bool>(sSql,false);
            return IsDBAuthorize(AppEnvironment.Session.USER_ID, function_name_, SecObjectType.Function);
        }
        internal static SalBoolean IsPresObjAvaliable(string identity_, string presObject, string securityObject, string objectType)
        {
            if (AppEnvironment.Session.IsRoot)
                return true;
            //SalString sSql = "SELECT SecuritySys_IsPresObjAvaliable('" + identity_ + "','" + pres_object_ + "');";
            //return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
            return IsAuthorize(identity_, presObject, securityObject, objectType);

        }
        public static SalBoolean IsPresObjAvaliable(SalString presObject, SalString securityObject, SalString objectType,SalString desc_)
        {
            if (AppEnvironment.Session.IsRoot)
            {
                fnd_object_security objSec = FndObjectFactory.CreateObjectSecurity(presObject, securityObject, SecObjectType.Window, objectType, desc_, "False");
                if (FndDAL.FindObjectSecurity(objSec) == null)
                {
                    FndDAL.ObjectSecurityNew(objSec);
                }
                return true;
            }
            //SalString sSql = "SELECT SecuritySys_IsPresObjAvaliable('" + identity_ + "','" + pres_object_ + "');";
            //return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
            return IsAuthorize(AppEnvironment.Session.USER_ID, presObject, securityObject, objectType);

        }
        internal static SalBoolean IsViewAvaliable(SalString identity_, SalString view_name_)
        {
            if (AppEnvironment.Session.IsRoot || cSessionManager.IsRemote)
                return true;
            //SalString sSql = "SELECT SecuritySys_IsViewAvaliable('" + identity_ + "','" + view_name_ + "');";
            //return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
            return IsDBAuthorize(identity_, view_name_, SecObjectType.View);
        }

        public static SalBoolean IsViewAvaliable(Type formType, SalString view_name_,SalString desc_)
        {
            if (AppEnvironment.Session.IsRoot)
            {
                fnd_object_security objSec = FndObjectFactory.CreateObjectSecurity(formType, view_name_, SecObjectType.Database, SecObjectType.View, desc_, "False");
                if (FndDAL.FindObjectSecurity(objSec) == null)
                {
                    FndDAL.ObjectSecurityNew(objSec);
                }
                return true;
            }
            //SalString sSql = "SELECT SecuritySys_IsViewAvaliable('" + identity_ + "','" + view_name_ + "');";
            //return cSessionManager.GetUserFunctionValue<bool>(sSql, false);
            return IsDBAuthorize(AppEnvironment.Session.USER_ID, view_name_, SecObjectType.View);
        }
        private static IUserSecurity Sec = null;
        internal static bool IsAuthorize(string identity_, string presObject, string securityObject, string objectType)
        {
            if (AppEnvironment.Session.IsRoot)
                return true;
            if( Sec == null)
                Sec = SecurityFactory.CreateSecurity();
            return Sec.Authorize(identity_, presObject, securityObject, objectType);
        }
        /// <summary>
        /// 数据库权限
        /// </summary>
        /// <param name="identity_">用户</param>
        /// <param name="securityObject">视图，表，函数等名称</param>
        /// <param name="objectType">视图，表，函数等类型</param>
        /// <returns></returns>
        internal static bool IsDBAuthorize(string identity_, string securityObject, string objectType)
        {
            if (AppEnvironment.Session.IsRoot || cSessionManager.IsRemote)
                return true;
            if (Sec == null)
                Sec = SecurityFactory.CreateSecurity();
            return Sec.DBAuthorize(identity_, securityObject, objectType);
        }
    }
}
